package cn.edu.sdjzu.xg.eagle.controller;


import cn.edu.sdjzu.xg.eagle.entity.Pagination;
import cn.edu.sdjzu.xg.eagle.entity.School;
import cn.edu.sdjzu.xg.eagle.exception.BysjException;
import cn.edu.sdjzu.xg.eagle.factory.ServiceFactory;
import cn.edu.sdjzu.xg.eagle.service.SchoolServiceImpl;
import cn.edu.sdjzu.xg.eagle.service.SchoolServiceInterface;
import cn.edu.sdjzu.xg.eagle.util.ControllerHelper;
import cn.edu.sdjzu.xg.eagle.util.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.JSONWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Collection;

@WebServlet("/SchoolController")
public class SchoolController extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        JSONObject message = new JSONObject();
        String id_str = req.getParameter("id");
        try{
            Long id_long = Long.parseLong(id_str);
            School school = ServiceFactory.getService(SchoolServiceInterface.class,"SchoolServiceImpl").find(id_long);
            String school_json = JSON.toJSONString(school, JSONWriter.Feature.ReferenceDetection);
            resp.getWriter().println(school_json);
        }catch(BysjException e){
            message.put("message",e.getMessage());
            resp.getWriter().println(message);
            e.printStackTrace();
        }catch (SQLException e){
            message.put("message","数据库操作异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        }catch (Exception e){
            message.put("message","网络异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        }
    }

    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        JSONObject message = new JSONObject();
        try {
            Collection<School> schoolList = SchoolServiceImpl.getInstance().findAll();
            String school_json = JSON.toJSONString(schoolList,JSONWriter.Feature.ReferenceDetection);
            resp.getWriter().println(school_json);
        }catch(BysjException e){
            message.put("message",e.getMessage());
            resp.getWriter().println(message);
            e.printStackTrace();
        }catch (SQLException e){
            message.put("message","数据库操作异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        }catch (Exception e){
            message.put("message","网络异常");
            resp.getWriter().println(message);
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        JSONObject message_jsonObj = new JSONObject();
        resp.setContentType("application/json;charset=UTF-8");
        String req_jsonStr = JSONUtil.getJSON(req);
        JSONObject req_jsonObj = JSON.parseObject(req_jsonStr);
        if (req_jsonObj == null){
            message_jsonObj.put("message","未收到请求消息！");
            resp.getWriter().println(message_jsonObj);
            return;
        }
        try{
            Pagination pagination = ControllerHelper.getPagination(req_jsonObj);
            Collection<School> schools = SchoolServiceImpl.getInstance().findAll(pagination);
            String schools_json = JSON.toJSONString(schools,JSONWriter.Feature.ReferenceDetection);
            message_jsonObj.put("totalNum",pagination.getTotalNum());
            message_jsonObj.put("data",schools_json);
            resp.getWriter().println(message_jsonObj);
        }catch(BysjException e){
            message_jsonObj.put("message",e.getMessage());
            resp.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (SQLException e){
            message_jsonObj.put("message","数据库操作异常");
            resp.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }catch (Exception e){
            message_jsonObj.put("message","网络异常");
            resp.getWriter().println(message_jsonObj);
            e.printStackTrace();
        }
    }
}
